﻿using System;
using System.Management;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a network ManagementObject to Network Class
    /// </summary>
    public static class NetworkTranslate
    {
        /// <summary>
        /// Convert Network ManagementObject to Network Class
        /// </summary>
        /// <param name="clsnet">Network class</param>
        /// <param name="queryObj">ManagementObject Group</param>
        /// <returns>NEtwork class with properties converted</returns>
        public static NetworkProperty Convert(this NetworkProperty clsnet, ManagementObject queryObj)
        {
            clsnet.Address = queryObj.PropertyValue<string>("Address");
            clsnet.AddressMask = queryObj.PropertyValue<string>("AddressMask");
            //item nof found (win 2003 and win2008)
            //clsnet.AutoMetric = queryObj.PropertyValue<bool>("AutoMetric");
            clsnet.Caption = queryObj.PropertyValue<string>("Caption");
            clsnet.Characteristics = queryObj.PropertyEnum<UInt32, CharacteristisNetwork>("Characteristics",CharacteristisNetwork.Unknown);
            clsnet.Description = queryObj.PropertyValue<string>("Description");
            clsnet.Flags = queryObj.PropertyValue<UInt32>("Flags");
            clsnet.InstallDate = queryObj.PropertyValue<DateTime?>("InstallDate");
            clsnet.Ipv4Addresses = queryObj.PropertyValue<string[]>("Ipv4Addresses");
            clsnet.Ipv4PrefixLengths = queryObj.PropertyValue<string[]>("Ipv4PrefixLengths");
            clsnet.Ipv6Addresses = queryObj.PropertyValue<string[]>("Ipv6Addresses");
            clsnet.Ipv6PrefixLengths = queryObj.PropertyValue<string[]>("Ipv6PrefixLengths");
            //item not found (win2003 and win2008)
            //clsnet.Metric = queryObj.PropertyValue<UInt32>("Metric");
            clsnet.Name = queryObj.PropertyValue<string>("Name");
            clsnet.PrivateProperties = new PrivatePropertiesData(queryObj);
            clsnet.Role = queryObj.PropertyEnum<UInt32, RolesNetwork>("Role",RolesNetwork.Both);
            clsnet.State = queryObj.PropertyEnum<UInt32,StateNetwork>("State",StateNetwork.Unknown);
            clsnet.Status = queryObj.PropertyEnum<string, StatusResource>("Status",StatusResource.Unknown);
            return clsnet;
        }

        ///<summary>
        /// Convert Network class to ManagementObject and Save on Cluster
        ///</summary>
        ///<param name="clsnet">Network class to converter</param>
        ///<param name="opSystem">Operation System where cluster is running</param>
        public static void SaveManagementObject(this NetworkProperty clsnet, WindowsOperationSystem opSystem)
        {
            if (opSystem == WindowsOperationSystem.Windows2003 || opSystem == WindowsOperationSystem.Windows2003R2)
            {
                //clsnet.Instance["AutoMetric"] = clsnet.AutoMetric;
                //item not found (windows 2003 and windows 2008)
            }
            clsnet.Instance["Characteristics"] = (UInt32)clsnet.Characteristics;
            clsnet.Instance["Description"] = clsnet.Description;
            clsnet.Instance["Flags"] = clsnet.Flags;
            clsnet.Instance["Characteristics"] = (UInt32)clsnet.Characteristics;
            if (opSystem == WindowsOperationSystem.Windows2003 || opSystem == WindowsOperationSystem.Windows2003R2)
            {
                //clsnet.Instance["Metric"] = clsnet.Metric;
                //item not found (windows 2003 and windows 2008)
            }
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsnet.Instance["PrivateProperties"] = clsnet.PrivateProperties.ConvertToManagemenBasetObject();
            }
            clsnet.Instance["Role"] = clsnet.Role;
            clsnet.Instance.Put();
        }


    }
}
